package com.jd.jdmall.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jd.jdmall.utils.UIUtils;

import butterknife.ButterKnife;

/**
 * Created by asus on 2017/2/14.
 *  所有Fragment的基类，在原有的Fragment基础上进行了简单的封装，使得所有子类具有上下文和Handle,
 *  并且明确了初始化数据和初始化事件的时机，提供两个“钩子”方法initData和initListener由子类选择性的实现，
 *  与直接继承Fragment要重写onCreateView方法类似，继承这个类也必须重写initView方法
 */

public abstract class BaseFragment extends Fragment {

    private static final String TAG = "BaseFragment";

    protected Context mContext;
    protected Handler mHandler;
    private Bundle mArgs;
    private View mRootView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mArgs = getArguments(); // 获取传递过来的参数
        init();
        mRootView = initView();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return mRootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this,view);  // 绑定
        initData();
    }



    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initListener();
    }




    // Fragment 的初始化
    protected void init(){
        mContext = getActivity();
        mHandler = UIUtils.getHandler();
    };

    // 初始化数据
    protected void initData(){

    }

    // 初始化事件
    protected void initListener(){

    };

    // 初始化控件
    protected abstract View initView() ;


}
